<template>
  <div class="app-container">


    <el-form :model="queryParams" ref="queryRef" :inline="true">

      <el-form-item label="所属行业" prop="industryId" style="width: 200px">
        <el-select v-model="queryParams.industryId" placeholder="请输入所属行业" filterable clearable>
          <el-option v-for="item in listIndustryArr" :key="item.industryId" :label="item.name"
            :value="item.industryId" />
        </el-select>
      </el-form-item>

      <el-form-item label="所属系统" prop="systemName">
        <el-input v-model="queryParams.systemName" placeholder="请输入所属系统" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item label="所属区域" prop="areaId" style="width: 200px">
        <el-cascader class="flex-1" v-model="queryParams.areaId" clearable :options="options" :props="props2"
          @change="TreeChange1" />
      </el-form-item>

      <el-form-item label="商家名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入商家名称" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="法人姓名" prop="legalPersonName">
        <el-input v-model="queryParams.legalPersonName" placeholder="请输入法人姓名" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入联系电话" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="Download" @click="handleExport">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="" icon="chatSquare" @click="handleNote">日志</el-button>
      </el-col>
    </el-row>

    <el-table border v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="55">
        <template #default="{ $index }">
          {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="商家名称" align="center" prop="name" />


      <el-table-column label="门面照片" width="100" align="center" prop="doorFace">
        <template #default="scope">
          <el-image style="width: 50px; height: 50px;" :src="scope.row.doorFace"
            :preview-src-list="[scope.row.doorFace]" preview-teleported>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="商家类型" align="center" prop="type">
        <template #default="scope">
          <span v-if="scope.row.type == 1">独立</span>
          <span v-if="scope.row.type == 2">加盟</span>
          <span v-if="scope.row.type == 3">连锁</span>
        </template>
      </el-table-column>
      <el-table-column label="软件版本" align="center" prop="merchantAppVersionName" />
      <el-table-column label="使用期限" align="center" prop="merchantAppVersionExpire" />

      <el-table-column label="预警设置" align="center" prop="merchantAppVersionExpireRemindDays" />
      <el-table-column label="预警提醒" align="center" prop="smsRemind" />


      <el-table-column label="所属行业" align="center" prop="industryName" />
      <el-table-column label="所属系统" align="center" prop="systemName" />
      <el-table-column label="所属区域" align="center" prop="addressArea" />
      <el-table-column label="单位地址" align="center" prop="addressDetail" />

      <el-table-column label="法人姓名" align="center" prop="legalPersonName" />
      <el-table-column label="法人电话" align="center" prop="legalPersonPhone" />
      <el-table-column label="单位电话" align="center" prop="phone" />
      <el-table-column label="邮箱" align="center" prop="email" />

      <el-table-column label="创建时间" align="center" prop="createTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="isClose">
        <template #default="scope">
          <span v-if="!scope.row.isClose">正常</span>
          <span v-if="scope.row.isClose">停业</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人员" align="center" prop="updateUserName" />



      <el-table-column fixed="right" label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="ZoomIn" @click="handleDetail(scope.row)">详情</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
      v-model:limit="queryParams.pageSize" @pagination="getList" />
    <!-- 添加或修改商家对话框 -->
    <el-dialog :title="title" v-model="open" width="780px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" :disabled="formDisable" label-width="130px">

        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="所属行业" prop="industryId">
              <el-select v-model="form.industryId" placeholder="请输入所属行业" filterable clearable>
                <el-option v-for="item in listIndustryArr" :key="item.industryId" :label="item.name"
                  :value="item.industryId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属系统">
              <el-select v-model="form.industrySystemId" placeholder="请选择">
                <el-option class="flex-1" v-for="item in systemList" :key="item.industrySystemId" :label="item.name"
                  :value="item.industrySystemId" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="12">
            <el-form-item label="商家名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入商家名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="法人手机号" prop="legalPersonPhone">
              <el-input v-model="form.legalPersonPhone" placeholder="请输入法人手机号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <!-- 1 独立 2 加盟 3 连锁 -->
              <el-select v-model="form.type" placeholder="请选择类型" filterable clearable>
                <el-option
                  v-for="item in [{ lable: '独立', value: 1 }, { lable: '加盟', value: 2 }, { lable: '连锁', value: 3 }]"
                  :key="item.value" :label="item.lable" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- merchantLinkName -->
            <el-form-item label="连锁机构" prop="merchantLinkName" v-if="form.type == '3' || form.type == '2'">
              <!-- filterable搜索  allow-create 创建新的选项  -->
              <el-select @change="sChange" v-model="form.merchantLinkName" default-first-option allow-create
                placeholder="请选择连锁机构" filterable clearable>
                <el-option v-for="item in listMerchantLink" :key="item.merchantLinkId" :label="item.name"
                  :value="item.merchantLinkId" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="法人姓名" prop="legalPersonName">
              <el-input v-model="form.legalPersonName" placeholder="请输入法人姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="法人身份证号" prop="legalPersonIdcardNum">
              <el-input v-model="form.legalPersonIdcardNum" placeholder="请输入法人身份证号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="办公场所" prop="officeSpace">
              <el-input v-model="form.officeSpace" placeholder="请输入办公场所" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="办公场所电话" prop="officeSpacePhone">
              <el-input v-model="form.officeSpacePhone" placeholder="请输入办公场所电话" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>

          <el-col :span="24">
            <el-form-item label="行业协会学会" prop="guild">
              <el-input v-model="form.guild" placeholder="请输入行业协会学会" />
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="所在地区" prop="addressArea">
              <!-- ccc -->
              <el-cascader class="flex-1" v-model="form.addressArea" :options="options" :props="props"
                @change="TreeChange2" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址详情" prop="addressDetail">
              <el-input v-model="form.addressDetail" placeholder="请输入地址详情" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="商家版本" prop="merchantAppVersionId">
              <el-select v-model="form.merchantAppVersionId" placeholder="请输入商家版本" filterable clearable>
                <el-option v-for="item in versionList" :key="item.merchantAppVersionId" :label="item.name"
                  :value="item.merchantAppVersionId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否停业" prop="isClose">
              <el-radio-group v-model="form.isClose">
                <el-radio v-for="dict in [
                  {
                    elTagClass: null,
                    elTagType: 'default',
                    label: '启用',
                    value: true
                  },
                  {
                    elTagClass: null,
                    elTagType: 'default',
                    label: '禁用',
                    value: false
                  },
                ]" :key="dict.value" :value="dict.value">{{
                  dict.label
                }}</el-radio>
              </el-radio-group>
            </el-form-item>

          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" />
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="申请人学历证书" prop="applicantEduCert">
              <ImageUpload v-model="form.applicantEduCert" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请人职业许可证" prop="applicantOccupationCert">
              <ImageUpload v-model="form.applicantOccupationCert" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="申请人其他证件" prop="applicantOtherCert">
              <ImageUpload v-model="form.applicantOtherCert" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请人职称证书" prop="applicantProfessionCert">
              <ImageUpload v-model="form.applicantProfessionCert" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="营业执照" prop="businessLicense">
              <ImageUpload v-model="form.businessLicense" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="医师执业许可证" prop="doctorLicense">
              <ImageUpload v-model="form.doctorLicense" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="门面照" prop="doorFace">
              <ImageUpload v-model="form.doorFace" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="消防安全许可证" prop="fireSafetyCertificate">
              <ImageUpload v-model="form.fireSafetyCertificate" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="行业许可证" prop="industryLicense">
              <ImageUpload v-model="form.industryLicense" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="食品安全许可证" prop="foodSafetyCertificate">
              <ImageUpload v-model="form.foodSafetyCertificate" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="法人身份证反面" prop="legalPersonIdcardBack">
              <ImageUpload v-model="form.legalPersonIdcardBack" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="法人身份证正面" prop="legalPersonIdcardFront">
              <ImageUpload v-model="form.legalPersonIdcardFront" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="药师执业许可证" prop="pharmacistLicense">
              <ImageUpload v-model="form.pharmacistLicense" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="其他相关证件" prop="otherCertificate">
              <ImageUpload v-model="form.otherCertificate" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>





  </div>
</template>

<script setup name="">
import {
  authApiindustrysystemlist,
} from "@/api/login.js";

const systemList = ref([])

function authApiindustrysystemlistApi(query = '') {
  authApiindustrysystemlist(query).then(
    (res) => {
      systemList.value = [...res];
    }
  );
}

authApiindustrysystemlistApi()

const data = reactive({
  form: {},
  queryParams: {
    currentPage: 1,
    pageSize: 10,
    name: undefined,
    phone: undefined,
    areaId: undefined,
    industryId: undefined,
  },
});

const { queryParams, form, rules } = toRefs(data);


// 获取行业
import {
  listIndustry
} from "@/api/backIndustry/backIndustry.js";
let listIndustryArr = ref([])
listIndustry().then((res) => {
  listIndustryArr.value = res.records
})

// 获取版本
import {
  getVersionList
} from "@/api/backMerchantAppVersion/backMerchantAppVersion.js";
let versionList = []
getVersionList().then((res) => {
  versionList = res.records
})
// 获取连锁机构
import {
  merchantLinkApiList
} from "@/api/backMerchantLink/backMerchantLink.js";
let listMerchantLink = []
merchantLinkApiList().then((res) => {
  listMerchantLink = res.records
})
// 改变连锁机构
const sChange = (e) => {
  if (listMerchantLink.find(i => i.merchantLinkId == e)) {
    form.value.merchantLinkId = e
    let merchantLinkName = listMerchantLink.find(i => i.merchantLinkId == e)?.name
    form.value.merchantLinkName = merchantLinkName
  } else {
    form.value.merchantLinkName = e
  }
}
// 获取地区
import {
  getAreaTree
} from "@/api/common";
let options = ref([])
getAreaTree().then((res) => {
  options.value = res
})

const props = { multiple: false, value: "areaId", label: "name", emitPath: true }
const props2 = { multiple: false, value: "areaId", label: "name", emitPath: true, checkStrictly: true }

function TreeChange1(e) {
  data.queryParams.areaId = `${e[e.length - 1]}`
}
function TreeChange2(e) {
  data.form.addressArea = `${e[e.length - 1]}`
}


import {
  addMerchant,
  delMerchant,
  getMerchant,
  listMerchant,
  updateMerchant,
} from "@/api/backMerchant/backMerchant";

const { proxy } = getCurrentInstance();
const { merchant_apply_status, sys_normal_disable, enable_status } = proxy.useDict(
  "merchant_apply_status",
  "sys_normal_disable",
  "enable_status"
);

const router = useRouter()
const route = useRoute()
const handleNote = () => {
  let menuId = route.meta.menuId
  router.push(`/system/log/operlog?data=${menuId}`)
}


const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

/** 查询商家列表 */
function getList() {
  loading.value = true;
  listMerchant(queryParams.value).then((response) => {
    noticeList.value = response.records;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    "addressArea": "",
    "merchantAppVersionI": "",
    "type": "",
    "merchantLinkId": "",
    "merchantLinkName": "",
    "addressDetail": "",
    "applicantEduCert": "",
    "applicantOccupationCert": "",
    "applicantOtherCert": "",
    "applicantProfessionCert": "",
    "businessLicense": "",
    "doctorLicense": "",
    "doorFace": "",
    "fireSafetyCertificate": "",
    "foodSafetyCertificate": "",
    "guild": "",
    "industryId": "",
    "industryLicense": "",
    "legalPersonIdcardBack": "",
    "legalPersonIdcardFront": "",
    "legalPersonIdcardNum": "",
    "legalPersonName": "",
    "legalPersonPhone": "",
    "name": "",
    "officeSpace": "",
    "officeSpacePhone": "",
    "otherCertificate": "",
    "pharmacistLicense": ""
  };
  proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.currentPage = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.merchantId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加商家";
}


// 禁用所有表单
const formDisable = ref(false)
/**详情按钮操作 */
function handleDetail(row) {
  reset();
  const merchantId = row.merchantId || ids.value;
  getMerchant(merchantId).then((response) => {
    form.value = response;
    open.value = true;
    title.value = "详情商家";
  });
  formDisable.value = true
}


/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const merchantId = row.merchantId || ids.value;
  getMerchant(merchantId).then((response) => {
    form.value = response;
    console.log(form.value, "v");

    open.value = true;
    title.value = "修改商家";
  });
  formDisable.value = false
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      if (form.value.merchantId != undefined) {
        updateMerchant(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMerchant(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const merchantIds = row.merchantId || ids.value;
  proxy.$modal
    .confirm('是否确认删除商家编号为"' + merchantIds + '"的数据项？')
    .then(function () {
      return delMerchant(merchantIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "/backAdmin/industryApi/industry/export",
    {
      ...queryParams.value,
    },
    `user_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>